package com.android.cattlefarm.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.android.cattlefarm.util.ImportEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Huang on 2015/7/4.
 */
public class ImportDB {
    public static String IMPORT_TABLE = "import_table";
    public static String COW_NUM = "cow_num";//注册牛号
    public static String COW_FARM_NUM = "cow_farm_num";//牛场号
    public static String COW_FARM_NAME = "cow_farm_name"; //牛场名
    public static String BIRTHDAY = "cow_birthday";//出生日期
    public static String TYRE = "cow_tyre"; //胎次
    public static String PRODUCTION_TIME = "production_time";//产犊日期
    public static String FATHER = "cow_father";//父号
    public static String MOTHER = "cow_mother";//母号
    public static String GRANDFATER = "cow_grandfather";//外祖父号

    SQLiteDatabase data;
    DBBaseHelper helper;
    Context context;
    Object object = new Object();
    public ImportDB(Context context) {
        if (helper == null) {
            helper = DBBaseHelper.getInstance(context);
        }
        data = helper.getWritableDatabase();
        this.context = context;

    }

    /**
     * 添加
     *
     * @param entities
     * @return
     */
    public long insertImport(List<ImportEntity> entities) {
        this.data.delete(IMPORT_TABLE, null, null);
        this.data.delete(AddDB.ADD_TABLE, null, null);
        long i = 0;
        data.beginTransaction();
        for (ImportEntity importEntity : entities) {
                ContentValues values = new ContentValues();
                values.put(COW_NUM, importEntity.getCowNum());
                values.put(COW_FARM_NUM, importEntity.getCowfarmNum());
                values.put(COW_FARM_NAME, importEntity.getCowfarmName());
                values.put(BIRTHDAY, importEntity.getBirthday());
                values.put(TYRE, importEntity.getTyre());
                values.put(PRODUCTION_TIME, importEntity.getProductionTime());
                values.put(FATHER, importEntity.getFather());
                values.put(MOTHER, importEntity.getMother());
                values.put(GRANDFATER, importEntity.getGrandfather());
                i = this.data.insert(IMPORT_TABLE, null, values);
        }
        data.setTransactionSuccessful();
        data.endTransaction();
        closeDataBase();
        return i;
    }

    public ArrayList<ImportEntity> queryImportList() {
        ArrayList<ImportEntity> entities = new ArrayList<ImportEntity>();

        String sql = "select * from "+IMPORT_TABLE;
        Cursor c = this.data.rawQuery(sql, null);
        if (c != null && c.getCount() > 0) {
            while (c.moveToNext()) {
                ImportEntity entity = new ImportEntity();
                entity.setCowNum(c.getString(c.getColumnIndex(COW_NUM)));
                entity.setCowfarmNum(c.getString(c.getColumnIndex(COW_FARM_NUM)));
                entity.setCowfarmName(c.getString(c.getColumnIndex(COW_FARM_NAME)));
                entity.setBirthday(c.getString(c.getColumnIndex(BIRTHDAY)));
                entity.setTyre(c.getString(c.getColumnIndex(TYRE)));
                entity.setProductionTime(c.getString(c.getColumnIndex(PRODUCTION_TIME)));
                entity.setFather(c.getString(c.getColumnIndex(FATHER)));
                entity.setGrandfather(c.getString(c.getColumnIndex(GRANDFATER)));
                entity.setMother(c.getString(c.getColumnIndex(MOTHER)));
                entities.add(entity);
            }
        }
        c.close();
        return entities;
    }

    public void closeDataBase() {
        if (data != null) {
            data.close();
        }
    }
}
